package org.jeecg.modules.shextdj.controller;

import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.shextdj.entity.AgShextdj;
import org.jeecg.modules.shextdj.entity.DetaShextData;
import org.jeecg.modules.shextdj.entity.DetaShextRecord;
import org.jeecg.modules.shextdj.service.IAgShextdjService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.tongxjl.entity.AgTongxjl;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 摄像头登记
 * @Author: jeecg-boot
 * @Date: 2020-09-14
 * @Version: V1.0
 */
@Api(tags = "摄像头登记")
@RestController
@RequestMapping("/shextdj/agShextdj")
@Slf4j
public class AgShextdjController extends JeecgController<AgShextdj, IAgShextdjService> {
    @Autowired
    private IAgShextdjService agShextdjService;

    @Autowired
    RestTemplate restTemplate;

    // 同步德塔摄像头数据
    @GetMapping(value = "/syncDeta")
    public Result <?> syncDeta() throws Exception {
        Map <String, Object> map = new HashMap <>();
        Map<String, Object> map2 = new HashMap<>();
        map2.put("page", 1);
        map2.put("size", 5000);
        map.put("pagination", map2);

        map2 = new HashMap<>();
        map2.put("direction", "DESC");
        List list2 = new ArrayList();
        list2.add("time");
        map2.put("fields", list2);
        map.put("sort", map2);
        map.put("t", DateUtils.getTimestamp());

        System.out.println(JSONObject.toJSON(map).toString());
        DetaShextData detaShextData = restTemplate.postForObject("http://26.29.23.93:8080/dw/sxtsy3344", map, DetaShextData.class);
        if(detaShextData != null) {
            agShextdjService.remove(null);

            System.out.println(detaShextData.getRecords().size());
            List batchList = new ArrayList();
            for (String str: detaShextData.getRecords()
            ) {
                DetaShextRecord detaShextRecord = JSON.parseObject(str, new TypeReference <DetaShextRecord>(){});
                if(detaShextData != null) {
                    AgShextdj agShextdj = new AgShextdj();

                    agShextdj.setCode(detaShextRecord.get_id());
                    agShextdj.setChangs(detaShextRecord.getManufacturer());
                    agShextdj.setShebmc(detaShextRecord.getPosition() + "_" + detaShextRecord.getManufacturer());

                    AgShextdj isExists = agShextdjService.getOne(new LambdaQueryWrapper<AgShextdj>().eq(AgShextdj::getShebmc, agShextdj.getShebmc()), false);
                    if(isExists != null)
                        continue;

                    agShextdj.setAnzwz(detaShextRecord.getPosition());
                    agShextdj.setGpsJingd(detaShextRecord.getLongitude());
                    agShextdj.setGpsWeid(detaShextRecord.getLatitude());
                    agShextdj.setAnzgd(detaShextRecord.getHeight());
                    agShextdj.setWs("ws://26.29.23.93:8086/" + URLEncoder.encode(detaShextRecord.getRtsp()));
                    agShextdj.setRtsp(detaShextRecord.getRtsp());
                    agShextdj.setBeiz(detaShextRecord.getRemarks());
                    agShextdj.setParentCode("1");

                    batchList.add(agShextdj);
                }
            }

            agShextdjService.saveBatch(batchList);

            return Result.ok();
        }

        return Result.error("同步数据失败");
    }

    @GetMapping(value = "/getTreeData")
    public Result<?> getTreeData(String parentCode) throws Exception {
        List<AgShextdj> list = agShextdjService.list(new QueryWrapper<AgShextdj>().eq("parent_code", parentCode));
        return Result.ok(list);
    }

    /**
     * 分页列表查询
     *
     * @param agShextdj
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "摄像头登记-分页列表查询")
    @ApiOperation(value = "摄像头登记-分页列表查询", notes = "摄像头登记-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(AgShextdj agShextdj,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<AgShextdj> queryWrapper = QueryGenerator.initQueryWrapper(agShextdj, req.getParameterMap());
        Page<AgShextdj> page = new Page<AgShextdj>(pageNo, pageSize);
        IPage<AgShextdj> pageList = agShextdjService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param agShextdj
     * @return
     */
    @AutoLog(value = "摄像头登记-添加")
    @ApiOperation(value = "摄像头登记-添加", notes = "摄像头登记-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AgShextdj agShextdj) {
        agShextdjService.save(agShextdj);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param agShextdj
     * @return
     */
    @AutoLog(value = "摄像头登记-编辑")
    @ApiOperation(value = "摄像头登记-编辑", notes = "摄像头登记-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody AgShextdj agShextdj) {
        agShextdjService.updateById(agShextdj);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "摄像头登记-通过id删除")
    @ApiOperation(value = "摄像头登记-通过id删除", notes = "摄像头登记-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        agShextdjService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "摄像头登记-批量删除")
    @ApiOperation(value = "摄像头登记-批量删除", notes = "摄像头登记-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.agShextdjService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "摄像头登记-通过id查询")
    @ApiOperation(value = "摄像头登记-通过id查询", notes = "摄像头登记-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        AgShextdj agShextdj = agShextdjService.getById(id);
        if (agShextdj == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(agShextdj);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param agShextdj
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AgShextdj agShextdj) {
        return super.exportXls(request, agShextdj, AgShextdj.class, "摄像头登记");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AgShextdj.class);
    }

}
